package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.ResEntElementTree;
import com.ctcemti.mdm.project.sys.service.IResEntElementTreeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "ResEntElementTreeController", description = "企业级ebs控制器类")
@RequestMapping("resEntElementTree")
public class ResEntElementTreeController {
    @Autowired
    private IResEntElementTreeService resEntElementTreeService;

    @ApiOperation(value = "查询企业级ebs树")
    @PostMapping("selectAllResEntElementTree")
    @ApiImplicitParams({
            @ApiImplicitParam(value="disciplinesId",name = "专业领域id"),
            @ApiImplicitParam(value="type",name = "查询类型"),
            @ApiImplicitParam(value="name",name = "名称"),
            @ApiImplicitParam(value="hierarchy",name = "层级"),
    })
    public Result<List<ResEntElementTree>> selectAllResEntElementTree(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){
        List<ResEntElementTree> list = resEntElementTreeService.selectAllResEntElementTree(resEntElementTree);
        return new Result<List<ResEntElementTree>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "通过id查询实体")
    @PostMapping("queryResEntElementTreeByID")
    @ApiImplicitParam(value="id",name = "id")
    public Result<ResEntElementTree> queryResEntElementTreeByID(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){
        ResEntElementTree resEntElementTreeObj = resEntElementTreeService.queryResEntElementTreeByID(resEntElementTree);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,resEntElementTreeObj);
    }

    @ApiOperation(value = "查询EBS是否存在")
    @PostMapping("queryEBSCodeHasExist")
    @ApiImplicitParams({
            @ApiImplicitParam(value="ebs",name = "ebs编码"),
            @ApiImplicitParam(value="id",name = "id")
    })
    public Result<ResEntElementTree> queryEBSCodeHasExist(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){

        int count = resEntElementTreeService.queryEBSCodeHasExist(resEntElementTree);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,count);
    }

    @ApiOperation(value = "插入")
    @PostMapping("insertResEntElementTreeByID")
    @ApiImplicitParam(value="ResEntElementTree",name = "EBS树实体类")
    public Result<ResEntElementTree>  insertResEntElementTreeByID(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        resEntElementTree.setCreator(loginId);
        int num = resEntElementTreeService.insertResEntElementTreeByID(resEntElementTree);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "修改")
    @PostMapping("updateResEntElementTreeByID")
    @ApiImplicitParam(value="ResEntElementTree",name = "EBS树实体类")
    public Result<ResEntElementTree>  updateResEntElementTreeByID(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        resEntElementTree.setModifiedBy(loginId);
        resEntElementTree.setModifyDate(new Date());
        resEntElementTreeService.updateResEntElementTreeByID(resEntElementTree);
        ResEntElementTree resEntElementTreeObj = resEntElementTreeService.queryResEntElementTreeByID(resEntElementTree);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,resEntElementTreeObj);
    }

    @ApiOperation(value = "删除")
    @PostMapping("deleteResEntElementTreeByID")
    @ApiImplicitParam(value="id",name = "EBS树id")
    public Result<ResEntElementTree>  deleteResEntElementTreeByID(  Integer[]  ids, HttpServletRequest request, HttpServletResponse response){
        resEntElementTreeService.deleteResEntElementTreeByID(ids);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "移动")
    @PostMapping("moveResEntElementTreeByID")
    @ApiImplicitParams({
            @ApiImplicitParam(value="id",name = "EBS树id"),
            @ApiImplicitParam(value="disciplinesId",name = "专业领域ID")
    })
    public Result<ResEntElementTree>  moveResEntElementTreeByID(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){
        int num= resEntElementTreeService.moveResEntElementTree(resEntElementTree);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "同步工法库")
    @PostMapping("syncResEntElementTree")
    @ApiImplicitParam(value="disciplinesId",name = "专业领域ID")
    public Result<ResEntElementTree>  syncResEntElementTree(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){
        int num= resEntElementTreeService.syncResEntElementTree(resEntElementTree);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,null);
    }
    @ApiOperation(value = "获取最大层级")
    @PostMapping("getMaxHierarchy")
    @ApiImplicitParam(value="disciplinesId",name = "专业领域ID")
    public Result<ResEntElementTree>  getMaxHierarchy(ResEntElementTree resEntElementTree, HttpServletRequest request, HttpServletResponse response){
        int num= resEntElementTreeService.getMaxHierarchy(resEntElementTree);
        return new Result<ResEntElementTree>(ResultEnum.SUCCESS,num);
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("uploadFile")
    @ApiImplicitParam(value="disciplinesId",name = "专业领域ID")
    public Result<Map<String,Object>> uploadFile(ResEntElementTree resEntElementTree,HttpServletRequest request, HttpServletResponse response) throws Exception {
        CommonsMultipartResolver commonsMultipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartFile multipartFile = null;
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
            Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
            while (iterator.hasNext()){
                multipartFile= multipartHttpServletRequest.getFile(iterator.next());
            }
        }
        Map<String,Object> result = resEntElementTreeService.uploadFile(multipartFile,resEntElementTree.getDisciplinesId(),response,request);
        return new Result<Map<String,Object>>(ResultEnum.SUCCESS,result);
    }
}
